﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SAP.Middleware.Connector;

namespace CM.DL.DALC.NC03
{
    public class DestConfig : IDestinationConfiguration
    {
        Dictionary<string, RfcConfigParameters> availableDestinations;
        RfcDestinationManager.ConfigurationChangeHandler changeHandler;

        public DestConfig()
        {
            availableDestinations = new Dictionary<string, RfcConfigParameters>();
        }

        public RfcConfigParameters GetParameters(string destinationName)
        {
            RfcConfigParameters foundDestination;
            availableDestinations.TryGetValue(destinationName, out foundDestination);
            //if (foundDestination == null)
            //{
            //    availableDestinations.Clear();
            //}
            return foundDestination;
        }

        //our configuration supports events
        public bool ChangeEventsSupported()
        {
            return true;
        }

        public event RfcDestinationManager.ConfigurationChangeHandler ConfigurationChanged
        {
            add
            {
                changeHandler = value;
            }
            remove
            {
                //do nothing
            }
        }

        //removes the destination that is known under the given name
        public void RemoveDestination(string name)
        {
            if (!string.IsNullOrEmpty(name) && availableDestinations.Remove(name))
            {
                //Console.WriteLine("Successfully removed destination " + name);
                //Console.WriteLine("Fire deletion event for destination " + name);
                RfcConfigurationEventArgs eventArgs = new RfcConfigurationEventArgs(RfcConfigParameters.EventType.DELETED);
                changeHandler(name, eventArgs);
                
            }
        }

        //allows adding or modifying a destination for a specific application server
        public void AddOrEditDestination(string name, int poolSize, string user, string password, string language, string client, string applicationServer, string systemNumber, string saprouter)
        {
            //in productive code the given parameters should be checked for validity, e.g. that name is not null
            //as this is not relevant for the example, we omit it here
            RfcConfigParameters parameters = new RfcConfigParameters();
            parameters[RfcConfigParameters.Name] = name;
            parameters[RfcConfigParameters.MaxPoolSize] = Convert.ToString(poolSize);
            parameters[RfcConfigParameters.IdleTimeout] = Convert.ToString(10); // we keep connections for 10 minutes
            parameters[RfcConfigParameters.User] = user;
            parameters[RfcConfigParameters.Password] = password;
            parameters[RfcConfigParameters.Client] = client;
            parameters[RfcConfigParameters.Language] = language;
            parameters[RfcConfigParameters.AppServerHost] = applicationServer;
            parameters[RfcConfigParameters.SystemNumber] = systemNumber;
            parameters[RfcConfigParameters.SAPRouter] = saprouter;
            RfcConfigParameters existingConfiguration;

            //if a destination of that name existed before, we need to fire a change event
            if (availableDestinations.TryGetValue(name, out existingConfiguration))
            {
                availableDestinations[name] = parameters;
                RfcConfigurationEventArgs eventArgs = new RfcConfigurationEventArgs(RfcConfigParameters.EventType.CHANGED, parameters);
                //MessageBox.Show("Fire change event " + eventArgs.ToString() + " for destination " + name);
                changeHandler(name, eventArgs);
            }
            else
            {
                availableDestinations[name] = parameters;
            }
            //MessageBox.Show("Added application server destination " + name);
        }

        //allows adding or modifying a destination for a specific application server
        public void AddOrEditDestination(RfcConfigParameters pRCP)
        {
            //in productive code the given parameters should be checked for validity, e.g. that name is not null
            //as this is not relevant for the example, we omit it here
            RfcConfigParameters parameters = pRCP;// new RfcConfigParameters();
            string name = pRCP[RfcConfigParameters.Name];
            RfcConfigParameters existingConfiguration;

            //if a destination of that name existed before, we need to fire a change event
            if (availableDestinations.TryGetValue(name, out existingConfiguration))
            {
                availableDestinations[name] = parameters;
                RfcConfigurationEventArgs eventArgs = new RfcConfigurationEventArgs(RfcConfigParameters.EventType.CHANGED, parameters);
                //MessageBox.Show("Fire change event " + eventArgs.ToString() + " for destination " + name);
                changeHandler(name, eventArgs);
            }
            else
            {
                availableDestinations[name] = parameters;
            }
            

            //MessageBox.Show("Added application server destination " + name);

        }
    }

    //public class InMemoryDestinationConfiguration : IDestinationConfiguration
    //{
    //    Dictionary<string, RfcConfigParameters> availableDestinations;
    //    RfcDestinationManager.ConfigurationChangeHandler changeHandler;

    //    public InMemoryDestinationConfiguration()
    //    {
    //        availableDestinations = new Dictionary<string, RfcConfigParameters>();
    //    }

    //    public RfcConfigParameters GetParameters(string destinationName)
    //    {
    //        RfcConfigParameters foundDestination;
    //        availableDestinations.TryGetValue(destinationName, out foundDestination);
    //        return foundDestination;
    //    }

    //    //our configuration supports events
    //    public bool ChangeEventsSupported()
    //    {
    //        return true;
    //    }

    //    public event RfcDestinationManager.ConfigurationChangeHandler ConfigurationChanged
    //    {
    //        add
    //        {
    //            changeHandler = value;
    //        }
    //        remove
    //        {
    //            //do nothing
    //        }
    //    }

    //    //removes the destination that is known under the given name
    //    public void RemoveDestination(string name)
    //    {
    //        if (!string.IsNullOrEmpty(name) && availableDestinations.Remove(name))
    //        {
    //            //Console.WriteLine("Successfully removed destination " + name);
    //            //Console.WriteLine("Fire deletion event for destination " + name);
    //            RfcConfigurationEventArgs eventArgs = new RfcConfigurationEventArgs(RfcConfigParameters.EventType.DELETED);
    //            changeHandler(name, eventArgs);
    //        }
    //    }

    //    //allows adding or modifying a destination for a specific application server
    //    public void AddOrEditDestination(string name, int poolSize, string user, string password, string language, string client, string applicationServer, string systemNumber)
    //    {
    //        //in productive code the given parameters should be checked for validity, e.g. that name is not null
    //        //as this is not relevant for the example, we omit it here
    //        RfcConfigParameters parameters = new RfcConfigParameters();
    //        parameters[RfcConfigParameters.Name] = name;
    //        parameters[RfcConfigParameters.MaxPoolSize] = Convert.ToString(poolSize);
    //        parameters[RfcConfigParameters.IdleTimeout] = Convert.ToString(1); // we keep connections for 10 minutes
    //        parameters[RfcConfigParameters.User] = user;
    //        parameters[RfcConfigParameters.Password] = password;
    //        parameters[RfcConfigParameters.Client] = client;
    //        parameters[RfcConfigParameters.Language] = language;
    //        parameters[RfcConfigParameters.AppServerHost] = applicationServer;
    //        parameters[RfcConfigParameters.SystemNumber] = systemNumber;
    //        RfcConfigParameters existingConfiguration;

    //        //if a destination of that name existed before, we need to fire a change event
    //        if (availableDestinations.TryGetValue(name, out existingConfiguration))
    //        {
    //            availableDestinations[name] = parameters;
    //            RfcConfigurationEventArgs eventArgs = new RfcConfigurationEventArgs(RfcConfigParameters.EventType.CHANGED, parameters);
    //            //MessageBox.Show("Fire change event " + eventArgs.ToString() + " for destination " + name);
    //            changeHandler(name, eventArgs);
    //        }
    //        else
    //        {
    //            availableDestinations[name] = parameters;
    //        }
    //        //MessageBox.Show("Added application server destination " + name);
    //    }

    //    //allows adding or modifying a destination for a specific application server
    //    public void AddOrEditDestination(RfcConfigParameters pRCP)
    //    {
    //        //in productive code the given parameters should be checked for validity, e.g. that name is not null
    //        //as this is not relevant for the example, we omit it here
    //        RfcConfigParameters parameters = pRCP;// new RfcConfigParameters();
    //        string name = pRCP[RfcConfigParameters.Name];
    //        RfcConfigParameters existingConfiguration;

    //        //if a destination of that name existed before, we need to fire a change event
    //        if (availableDestinations.TryGetValue(name, out existingConfiguration))
    //        {
    //            availableDestinations[name] = parameters;
    //            RfcConfigurationEventArgs eventArgs = new RfcConfigurationEventArgs(RfcConfigParameters.EventType.CHANGED, parameters);
    //            //MessageBox.Show("Fire change event " + eventArgs.ToString() + " for destination " + name);
    //            changeHandler(name, eventArgs);
    //        }
    //        else
    //        {
    //            availableDestinations[name] = parameters;
    //        }

    //        //MessageBox.Show("Added application server destination " + name);

    //    }
    //}
}
